#include "../../Server.hpp"
#pragma once
class EchoServer
{
private:
    void EstablishHandler(ConnectionPtr conn)
    {
        DEBUG_LOG("new connection:%p", conn.get());
    }

    void MessageHandler(ConnectionPtr conn, Buffer *inbuffer)
    {
        // 发送回去
        conn->SendInLoop(inbuffer->ReadPtr(),inbuffer->ReadSize());
        inbuffer->UpdateReadOffset(inbuffer->ReadSize());
        conn->DisConnectInLoop(); // 发送完消息主动关闭连接
    }

    void CloseHandler(ConnectionPtr conn)
    {
        DEBUG_LOG("close conn %p", conn.get());
    }

public:
    EchoServer(uint16_t port, size_t slave_loop_cnt) : _tcp_server(port, slave_loop_cnt)
    {
        _tcp_server.SetEstablishCallback(std::bind(&EchoServer::EstablishHandler, this, std::placeholders::_1));
        _tcp_server.SetMsgHandlerCallback(std::bind(&EchoServer::MessageHandler, this, std::placeholders::_1, std::placeholders::_2));
        _tcp_server.SetReleaseCallback(std::bind(&EchoServer::CloseHandler, this, std::placeholders::_1));
    }
    void Start()
    {
        _tcp_server.Start();
    }

private:
    TcpServer _tcp_server;
};